/**
 *
 */
package picounit.java.type;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;

import picounit.java.Factory;
import picounit.java.collection.Filter;
import picounit.java.exceptions.IllegalAccess;
import picounit.java.exceptions.InvalidArguments;
import picounit.java.exceptions.Uninvokable;

public class Method {
    public static final Factory<Method, java.lang.reflect.Method> FACTORY =
        new Factory<Method, java.lang.reflect.Method>() {
            public Method create(java.lang.reflect.Method method) {
                return new Method(method);
            }
        };

    public static final Filter<Method> STATIC_FILTER = new Filter<Method>() {
        public boolean passes(Method method) {
            return method.isStatit();
        }
    };

    private Method(java.lang.reflect.Method method) {
        this.method = method;
    }

    public Type declaringType() {
        return new Type(method.getDeclaringClass());
    }

    public boolean isStatit() {
        return Modifier.isStatic(method.getModifiers());
    }

    public Object invoke(Object target, Object ... arguments) {
        try {
            return method.invoke(target, arguments);
        }
        catch (IllegalArgumentException e) {
            throw new InvalidArguments(this, arguments);
        }
        catch (IllegalAccessException e) {
            throw new IllegalAccess(this, arguments);
        }
        catch (InvocationTargetException e) {
            throw new Uninvokable(this, arguments);
        }
    }

    private final java.lang.reflect.Method method;
}